package com.example.firstdemo.ui.screens.pages.mine

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil3.compose.AsyncImage
import com.example.firstdemo.Config
import com.example.firstdemo.R
import com.example.firstdemo.ui.screens.components.Dialog
import com.example.firstdemo.ui.screens.pages.mine.data.LocalMineDataProvider
import com.example.firstdemo.ui.screens.pages.mine.data.MinePageUIState
import com.example.firstdemo.ui.theme.AppTheme
import com.example.firstdemo.ui.theme.FontSize
import com.example.firstdemo.ui.theme.Space
import com.example.firstdemo.ui.theme.eColors
import com.example.firstdemo.ui.theme.full

@Composable
fun ListItem(
    label: @Composable () -> Unit,
    value: @Composable () -> Unit,
    modifier: Modifier = Modifier
) {
    Row(
        horizontalArrangement = Arrangement.SpaceBetween,
        modifier = modifier
            .fillMaxWidth()
            .background(
                MaterialTheme.eColors.background.base
            )
            .padding(Space.medium)
    ) {
        label()
        value()
    }
}

@Composable
fun MineScreen(
    padding: PaddingValues = PaddingValues(0.dp),
    uiState: MinePageUIState,
    onLogout: () -> Unit = {},
    onSetShowDialog: (show: Boolean) -> Unit = {}
) {
    val userInfo = uiState.userInfo
    fun formatValue(value: String?): String? {
        return if ((value ?: "") == "") "无" else value
    }
    Column(
        Modifier
            .padding(padding)
            .fillMaxSize()
            .background(MaterialTheme.eColors.background.page)
    ) {
        Box(
            Modifier
                .fillMaxWidth()
                .wrapContentHeight()
                .background(MaterialTheme.eColors.primary.light5)
        ) {
            Row(
                Modifier
                    .height(IntrinsicSize.Min)
                    .padding(Space.large)
            ) {
                if ((userInfo?.avatar ?: "") != "") {
                    AsyncImage(
                        model = "${Config.BASE_URL}${userInfo?.avatar?.removePrefix("/")}",
                        contentDescription = "",
                        modifier = Modifier
                            .size(80.dp)
                            .clip(MaterialTheme.shapes.full)
                    )
                } else {
                    Image(
                        painter = painterResource(R.drawable.mine_avatar),
                        contentDescription = "",
                        modifier = Modifier
                            .size(80.dp)
                            .clip(MaterialTheme.shapes.full)
                    )
                }
                Spacer(Modifier.width(Space.medium))
                Column(
                    verticalArrangement = Arrangement.Center,
                    modifier = Modifier.fillMaxSize()
                ) {
                    Text(
                        text = userInfo?.nickName ?: "",
                        fontSize = FontSize.large,
                        color = MaterialTheme.eColors.basic.white
                    )
                    Text(
                        text = "用户名：${userInfo?.userName ?: ""}",
                        fontSize = FontSize.medium,
                        color = MaterialTheme.eColors.basic.white
                    )
                }
            }
        }
        Box(
            Modifier
                .fillMaxSize()
                .padding(Space.medium)
        ) {
            Column {
                Card {
                    ListItem(
                        label = { Text("电话") },
                        value = { Text("${formatValue(userInfo?.phoneNumber)}") }
                    )
                    HorizontalDivider(thickness = 1.dp, color = MaterialTheme.eColors.border.base)
                    ListItem(
                        label = { Text("邮箱") },
                        value = { Text("${formatValue(userInfo?.email)}") }
                    )
                }
                Spacer(Modifier.height(Space.small))
                Button(
                    shape = MaterialTheme.shapes.small,
                    colors = ButtonDefaults.buttonColors(
                        containerColor = MaterialTheme.eColors.background.base,
                        contentColor = MaterialTheme.eColors.danger.base,
                    ),
                    border = BorderStroke(1.dp, MaterialTheme.eColors.danger.base),
                    modifier = Modifier.fillMaxWidth(),
                    onClick = {
                        onSetShowDialog(true)
                    }
                ) {
                    Text(
                        text = "退出登录",
                        color = MaterialTheme.eColors.danger.base
                    )
                }
            }
        }
    }
    val showDialog = uiState.showDialog
    if (showDialog) {
        Dialog(
            title = "警告",
            info = "确认退出？",
            onConfirm = {
                onSetShowDialog(false)
                onLogout()
            },
            onDismiss = {
                onSetShowDialog(false)
            }
        )
    }
}


@Preview(
    showBackground = true,
    showSystemUi = true
)
@Composable
fun GreetingPreview() {
    AppTheme {
        MineScreen(uiState = LocalMineDataProvider.uiState)
    }
}

@Preview(
    showBackground = true,
)
@Composable
fun ListItemPreview() {
    AppTheme {
        ListItem(
            label = { Text("邮箱") },
            value = { Text("34221680@qq.com") }
        )
    }
}